package com.appbee.news.client.activty;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.appbee.news.shared.NewsListResultProxy;
import com.appbee.news.shared.NewsProxy;
import com.appbee.news.shared.NewsRequestFactory;
import com.appbee.news.shared.NewsServiceRequest;
import com.appbee.user.client.AccessControl;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.google.web.bindery.requestfactory.shared.Violation;

@Singleton
public class NewsActivty extends AbstractActivity implements IPresenter
{
	public interface View extends IsWidget
	{
		public void setTotalSize(int totalSize);
		
		public void showNews(int start,List<NewsProxy> list);
		
		public void setPresenter(IPresenter listener);

		public void showViolations(Set<Violation> errors);
	}

	private final View view;
	private NewsServiceRequest request;
	private final NewsRequestFactory requestProvider;
	private final int viewSize = 20;
	private final ListReceiver listReceiver = new ListReceiver();
	private final AccessControl accessControl;
	
	@Inject
	public NewsActivty(View view, NewsRequestFactory requestProvider,
			AccessControl accessControl)
	{
		this.view = view;
		this.accessControl = accessControl;
		this.view.setPresenter(this);
		this.requestProvider = requestProvider;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus)
	{
		Request<NewsListResultProxy> request = requestProvider.getNewsService()
				.findByRange(0, viewSize);
		request.fire(listReceiver);
		panel.setWidget(view);
	}

	@Override
	public void onPublishNews(String title, String content)
	{
		NewsServiceRequest newsRequest = requestProvider.getNewsService();
		NewsProxy news = newsRequest.create(NewsProxy.class);
		news.setTitle(title);
		news.setContent(content);
		
		
		newsRequest.publish(news, viewSize).fire(listReceiver);
	}

	@Override
	public boolean isEditMode()
	{
		return accessControl.isMemberOrAdmin();
	}

	@Override
	public void onPublishNews(NewsProxy newsProxy)
	{
		request.publish(newsProxy, viewSize).fire(listReceiver);
		
	}

	@Override
	public void onDelete(NewsProxy news)
	{
		requestProvider.getNewsService()
				.removeAndFindByRange(news, 0, viewSize).fire(listReceiver);
		
	}

	@Override
	public NewsProxy edit(NewsProxy newsProxy)
	{
		request = requestProvider.getNewsService();
		return request.edit(newsProxy);
	}
	
	public class ListReceiver extends Receiver<NewsListResultProxy>
	{

		@Override
		public void onSuccess(NewsListResultProxy response)
		{
			view.showNews(0, response.getList());
			view.setTotalSize(response.getTotalCount());
		}

		@Override
		public void onViolation(Set<Violation> errors)
		{
			view.showViolations(errors);
		}

		@Override
		public void onFailure(ServerFailure error)
		{
			super.onFailure(error);
		}
		
	};

}
